#include "my_pwm_output.hpp"
void my_TIM1_pwm_output::TIM1_PWM_init(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    TIM_BDTRInitTypeDef TIM1_BDTRInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    /**********TIM1定时器时基配置**************/
    TIM_TimeBaseStructure.TIM_Period = 3000;                    //计数周期
    TIM_TimeBaseStructure.TIM_Prescaler = 0;                    //分频系数
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     // CKD分频  配置死区时间会用到
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // TIM_CounterMode_CenterAligned1
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

    /**********输出比较结构体初始化**************/
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;               //配置为PWM模式1
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;   //使能CHx的PWM输出
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //互补输出使能，使能CHxN的PWM输出
    TIM_OCInitStructure.TIM_Pulse = 10;                             //设置占空比大小

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;       //输出通道有效极性
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_Low;      //互补输出通道有效极性
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;   //输出通道空闲极性（刹车）
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //  //互补输出通道空闲极性（刹车）

    TIM_OC1Init(TIM1, &TIM_OCInitStructure); //配置U--CH1
    TIM_OCInitStructure.TIM_Pulse = 10;
    TIM_OC2Init(TIM1, &TIM_OCInitStructure); //配置V--CH2
    TIM_OCInitStructure.TIM_Pulse = 10;
    TIM_OC3Init(TIM1, &TIM_OCInitStructure); //配置W--CH3

    /**********死区与刹车初始化**************/
    TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
    TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
    TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
    //设置死区时间 DTG= 80  = 0101 0000，
    TIM1_BDTRInitStructure.TIM_DeadTime = 5;
    //刹车配置：当BKIN引脚检测为高电平时，输出信号被禁止，就像刹车一样
    TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
    TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
    TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
    TIM_BDTRConfig(TIM1, &TIM1_BDTRInitStructure);
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // CCR4这个输出使能后才能用来触发ADC1的注入通道采样
    TIM_OCInitStructure.TIM_Pulse = 10;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
    TIM_OC4Init(TIM1, &TIM_OCInitStructure);

    // enable interrupt
    TIM_InternalClockConfig(TIM1);
    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
    TIM_ITConfig(TIM1, TIM_IT_CC3, ENABLE);
    TIM_ITConfig(TIM1, TIM_IT_CC4, ENABLE);
    // CCR4的中断，这个通过设置CCR4的pulse来控制产生中断相当于PWM-ON的位置
    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
    TIM_ITConfig(TIM1, TIM_IT_COM, ENABLE);
    // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //4个抢先级、4个子优先级
    // NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
    // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    // NVIC_Init(&NVIC_InitStructure);
    // TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //使能中断
    TIM_UpdateDisableConfig(TIM2, DISABLE);
    TIM_CtrlPWMOutputs(TIM1, ENABLE);   // PWM输出使能（高级定时器需要，通用不需要）
    TIM_CCPreloadControl(TIM1, ENABLE); //
    TIM_Cmd(TIM1, ENABLE);              //使能TIM1
}
void my_TIM1_pwm_output::TIM1_GPIO_INIT(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; // PWM1 (U)CH1--PA8; (V)CH2--PA9; (W)CH3--PA10
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; // CH1N--PB13   CH2N--PB14   CH2N--PB15
    GPIO_Init(GPIOB, &GPIO_InitStruct);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; // PWM1 (U)CH1--PA8; (V)CH2--PA9; (W)CH3--PA10
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void my_TIM1_pwm_output::TIM1_CCR_change(uint32_t ccr1, uint32_t ccr2, uint32_t ccr3)
{
    TIM1->CCR1 = ccr1;
    TIM1->CCR2 = ccr2;
    TIM1->CCR3 = ccr3;
}
void my_TIM1_pwm_output::TIM1_ADC_trigger_init(void)
{
    ADC_InitTypeDef ADC_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_InitStructure.ADC_ScanConvMode = ENABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; // ADC由CC1事件触发
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfChannel = 8;
    ADC_Init(ADC1, &ADC_InitStructure);
    ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
    ADC_TempSensorVrefintCmd(ENABLE);

    ADC_Cmd(ADC1, ENABLE);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_7Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_7Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_7Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_7Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_7Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_7Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_7Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_7Cycles5);

    ADC_DMACmd(ADC1, ENABLE);
    ADC_ExternalTrigConvCmd(ADC1, ENABLE); //设置外部触发模式使能
    ADC_ResetCalibration(ADC1);            //复位校准寄存器
    while (ADC_GetResetCalibrationStatus(ADC1))
        ;                       //等待校准寄存器复位完成
    ADC_StartCalibration(ADC1); // ADC校准
    while (ADC_GetCalibrationStatus(ADC1))
        ; //等待校准完成
}
void my_TIM1_pwm_output::TIM1_ADC_DMA_init(void)
{
    DMA_InitTypeDef DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* DMA1 Channel1 Configuration ----------------------------------------------*/
    DMA_DeInit(DMA1_Channel1);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)my_TIM1_pwm_output::AD_Value;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = 8;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);

    DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);

    /* Enable DMA1 channel1 */
    DMA_Cmd(DMA1_Channel1, ENABLE);
}